<?php
/**
 *问答社区问题页控制器
 */
class Commwapquestion extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('commwap_model');
    }
    public function readQuestion()
    {
        $post = $this->input->post();
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error('error: lack question id');
        else $qid = (int)$post['qid'];
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        $this->commwap_model->readQuestion($qid,$uid);
        $this->success();
    }
    public function index()
    {
        $post = $this->input->post();
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error('error: lack question id');
        else $qid = (int)$post['qid'];
        if (!isset($post['uid'])||!is_numeric($post['uid'])) $this->error('error: lack user id');
        else $uid = (int)$post['uid'];
        $qdata  = $this->commwap_model->getQWithUserById($qid,$uid);
        $this->success($qdata);
    }
    public function getQContributer()
    {
        $post = $this->input->post();
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error('error: lack question id');
        else $qid = (int)$post['qid'];
        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];
        $condata  = $this->commwap_model->getQContributer($qid,$limit);
        $this->success($condata);
    }
    public function getAnswer()
    {
        $post = $this->input->post();
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error('error: lack question id');
        else $qid = (int)$post['qid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        if (isset($post['sort'])) $sort = $post['sort'];
        else $sort = 'score';

        $res = $this->commwap_model->getAnswersByqid($qid,$offset,$limit,$sort);
        $this->success($res);
    }
    public function followQuestion()
    {
        $qid = $this->input->input_stream('qid');
        $uid = $this->input->input_stream('uid');
        if (!isset($qid)||!is_numeric($qid)) $this->error('error: lack question id');
        else $qid = (int)$qid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $this->commwap_model->followQuestion($qid,$uid);
        $this->success();
    }

    public function cancelfollowQuestion()
    {
        $qid = $this->input->input_stream('qid');
        $uid = $this->input->input_stream('uid');
        if (!isset($qid)||!is_numeric($qid)) $this->error('error: lack question id');
        else $qid = (int)$qid;
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        $this->commwap_model->cancelfollowQuestion($qid,$uid);
        $this->success();
    }

    public function addQuestion()
    {
        $uid = $this->input->input_stream('uid');
        $subid = $this->input->input_stream('subid');
        $question = $this->input->input_stream('question');
        $description = $this->input->input_stream('description');

        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        if (!isset($subid)||!is_numeric($subid)) $this->error('error: lack subject id');
        else $subid = (int)$subid;

        $score = round(time()/10000);
        $data = array('uid'=>$uid,'question'=>$question,'description'=>$description,
            'score'=>$score,'created'=>time(),'changed'=>time());
        $subdata = array('subid'=>$subid,'created'=>time());

        $res = $this->commwap_model->addQuestion($data,$subdata);

        if ($res!==false) $this->success($res);
        else $this->error('系统错误');
    }

    public function getSubject()
    {
        $this->success($this->commwap_model->getEditSubject());
    }

}